package com.cskaoyan.service.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketCart;
import com.cskaoyan.bean.po.MarketCouponUser;
import com.cskaoyan.bean.wx.coupon.WxCouponListListVO;
import com.cskaoyan.bean.wx.coupon.WxCouponListVO;
import com.cskaoyan.bean.wx.coupon.WxCouponMylistListVO;
import com.cskaoyan.bean.wx.coupon.WxCouponMylistVO;
import com.cskaoyan.mapper.MarketCartMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.wx.WxCouponMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author chenai
 * @description TODO
 * @date 2022-06-07 21:40
 */
@Service
public class WxCouponServiceImpl implements WxCouponService {
    @Autowired
    WxCouponMapper wxCouponMapper;

    @Autowired
    MarketCartMapper marketCartMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    WxCartService wxCartService;
    @Override
    public WxCouponMylistVO mylist(BaseParam baseParam, Integer status , Integer userId) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<WxCouponMylistListVO> wxCouponMylistListVOS = wxCouponMapper.quaryCouponByUserId(userId, status);
        for (WxCouponMylistListVO wxCouponMylistListVO : wxCouponMylistListVOS) {
            wxCouponMylistListVO.setAvailable(false);
        }
        PageInfo pageInfo = new PageInfo(wxCouponMylistListVOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        WxCouponMylistVO wxCouponMylistVO = new WxCouponMylistVO(baseParam.getPage(),baseParam.getLimit(),total,pages,wxCouponMylistListVOS);
        return wxCouponMylistVO;
    }

    @Override
    public BaseRespVo receive(Integer couponId,Integer userId) {
        int i = wxCouponMapper.queryUserCouponCountById(couponId,userId);
        if (i == 0) {
            addCoupon(couponId, userId);
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.couponRecieved();
        }
    }

    @Override
    public WxCouponListVO list(BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        List<WxCouponListListVO> wxCouponListListVOS = wxCouponMapper.quaryCouponList();
        PageInfo pageInfo = new PageInfo(wxCouponListListVOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        WxCouponListVO wxCouponListVO = new WxCouponListVO(baseParam.getPage(),baseParam.getLimit(),total,pages,wxCouponListListVOS);
        return wxCouponListVO;
    }

    @Override
    public BaseRespVo exchange(Integer userId, String code) {
        Integer couponId = wxCouponMapper.queryCouponIdByCode(code);
        if (couponId == null){
            return BaseRespVo.couponNotFound();
        }else {
            int i = wxCouponMapper.queryUserCouponCountById(couponId,userId);
            if (i == 0) {
                addCoupon(couponId, userId);
                return BaseRespVo.ok();
            } else {
                return BaseRespVo.couponRecieved();
            }
        }
    }

    @Override
    public WxCouponMylistVO selectList(Integer userId,Integer cartId,Integer groupOnRulesId) {
        List<WxCouponMylistListVO> wxCouponMylistListVOS = wxCouponMapper.quaryCouponByUserId(userId, 0);
        MarketCart marketCart = marketCartMapper.selectByPrimaryKey(cartId);
        List<MarketCart> checkedGoodsList = wxCartService.queryCartListByUserIdAndChecked(userId, true);
        //goodsTotalPrice是根据checkedGoodsList计算总金额
        BigDecimal goodsTotalPrice = BigDecimal.ZERO;
        if (checkedGoodsList != null) {
            for (MarketCart marketCart1 : checkedGoodsList) {
                goodsTotalPrice = goodsTotalPrice.add(marketCart1.getPrice().multiply(new BigDecimal(Integer.toString(marketCart1.getNumber()))));
            }
        }
        for (WxCouponMylistListVO wxCouponMylistListVO : wxCouponMylistListVOS) {
            if (wxCouponMylistListVO.getEndTime().before(new Date())) {
                wxCouponMylistListVO.setAvailable(false);
            } else if (wxCouponMylistListVO.getStartTime().after(new Date())) {
                wxCouponMylistListVO.setAvailable(false);
            } else if (goodsTotalPrice.compareTo(wxCouponMylistListVO.getMin()) < 0) {
                wxCouponMylistListVO.setAvailable(false);
            } else {
                wxCouponMylistListVO.setAvailable(true);
            }
        }
        PageInfo pageInfo = new PageInfo(wxCouponMylistListVOS);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        WxCouponMylistVO wxCouponMylistVO = new WxCouponMylistVO(pages, ((int) total),total,pages,wxCouponMylistListVOS);
        return wxCouponMylistVO;
    }

    private void addCoupon(Integer couponId, Integer userId) {
        Date date = new Date();
        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser = wxCouponMapper.quaryCouponInfoForUserByCouponId(couponId);
        marketCouponUser.setUserId(userId);
        marketCouponUser.setAddTime(date);
        marketCouponUser.setUpdateTime(date);
        marketCouponUser.setStatus(((short) 0));
        if(wxCouponMapper.getTimeType(couponId)==1){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE,wxCouponMapper.getDays(couponId));
            marketCouponUser.setStartTime(date);
            marketCouponUser.setEndTime(calendar.getTime());
        }
        marketCouponUserMapper.insert(marketCouponUser);
    }
}
